/**
 * 3η ΓΕ 2013-2014 - Super Market
 * ΦΡΑΓΚΟΥΛΟΠΟΥΛΟΥ ΕΜΜΑΝΟΥΕΛΑ (69298)
 * ΧΑΛΔΑΣ ΧΑΡΑΛΑΜΠΟΣ (69305)
 * ΧΟΡΜΟΒΙΤΗΣ ΙΩΑΝΝΗΣ (69325)
 * ΧΟΥΣΤΟΥΛΑΚΗ ΕΥΑΓΓΕΛΙΑ (69327)
 */
package customerGUI;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import model.*;
import supermarket.EntityManagerFactory;

/**
 * Class Καλαθιού Αγορών 
 */
public class Basket {

    private static Basket myInstance = null;
    private List<ProductPurchase> pplist;
    private Store myStore;

    private Basket() {
        pplist = new ArrayList<ProductPurchase>();
    }

    public void add(ProductPurchase p) {
        pplist.add(p);
    }

    public void clear() {
        pplist.clear();
    }

    public int size() {
        return pplist.size();
    }

    public List<ProductPurchase> getList() {
        return pplist;
    }

    public static Basket getInstance() {
        if (myInstance == null) {
            myInstance = new Basket();
        }
        return myInstance;
    }

    public void initStore(Store s) {
        if (pplist.size() != 0 && myStore != null
                && !s.getName().equals(myStore.getName())) {
            clear();
            System.out.println("Store cleared");
        }
        myStore = s;
    }

    public String getStatus() {
        return "Κατάστημα " + myStore.getName() + " , # Παραγγελιών " + size();
    }

    public void goPay(Customer c, List<Cheques> cheques, boolean sendAtHome) {
        float totalPrice = 0;
        int pointsEarned = 0;
        for (ProductPurchase pp : pplist) {
            int q = pp.getQuantity();
            totalPrice += pp.getProductId().getPrice() * q;
            pointsEarned += pp.getProductId().getPoints() * q;
        }

        List<Cheques> chequesUsed = new ArrayList<>();
        // the actual checks we used goes in checksUsed
        float price = totalPrice;
        for (int i = 0; i < cheques.size(); ++i) {
            if (price >= 6) {
                chequesUsed.add(cheques.get(i));
            }
            price -= 6;
        }

        int oldPoints = c.getAvailiablePoints() != null ? c.getAvailiablePoints() : 0;
        int totalPoints = oldPoints + pointsEarned;
        int newChecksCnt = pointsEarned / 200;
        

        // this is total points we ever got
        c.setAvailiablePoints(totalPoints);

        Purchase p = new Purchase();
        p.setDelivery(sendAtHome);
        p.setAmount(price);
        p.setCustomer(c);
        p.setDatetime(new java.util.Date());

        //p.setProductPurchaseCollection(pplist);
        p.setPointsEarned(pointsEarned);
        p.setStore(myStore);

        EntityManager em = EntityManagerFactory.getEntityManager();
        em.getTransaction().begin();
        // save each product purchase
        for (ProductPurchase pp : pplist) {
            pp.setPurchaseId(p);
            em.persist(pp);
        }
        // save purchase
        em.persist(p);
        // save customer fields we update
        em.persist(c);
        // set used check as invalid
        for (int i = 0; i < chequesUsed.size(); ++i) {
            System.out.println("Use check " + chequesUsed.get(i).getChequeId());
            chequesUsed.get(i).setIsvalid(false);
        }
        // create new checks
        for (int i = 0; i < newChecksCnt; ++i) {
            Cheques chk = new Cheques();
            chk.setCreationDate(new Date());
            chk.setCustomerId(c);
            chk.setIsvalid(true);
            em.persist(chk);
        }
        em.getTransaction().commit();
    }

}
